#include <iostream>
#include "log.h"

logger g_log;


logger::logger():
    m_exit(false),
    m_log_prio(0)
{
}

void logger::initlog(const std::string &_info, int _log_prio)
{
    openlog("asud", 0, LOG_DAEMON);
    m_log_prio = _log_prio;
    
}
		
void logger::msg(int _prio, const std::string &_msg)
{
    if (_prio < m_log_prio)
    {
	boost::mutex::scoped_lock lck(m_condition_mutex);
	m_queue.push(_msg);
	m_condition.notify_one();			
    }
}

void logger::msg(int _prio, format _fmt)
{
    msg(_prio, _fmt.str());
}
	
void logger::run()
{
    std::string buffer;	
    
    while (!m_exit)
    {

		{
    	    boost::mutex::scoped_lock lck(m_condition_mutex);

    	    while (m_queue.size() == 0)
    	    {
        	  m_condition.wait(lck);

			  if (m_exit)
			  {
		  		return;
			  }
	  		}	

	  		buffer  = m_queue.front();
    	    m_queue.pop();
    	}

    	syslog(LOG_INFO, "%s", buffer.c_str());
    	
    	#if !defined(NDEBUG)
    	std::cout<< "Msg:" << buffer << std::endl;
    	#endif
    }	
}
	
void logger::stop()
{
    m_exit = true;
    m_condition.notify_one();
}

